package com.app.teaware.goods;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BasisBean;
import com.app.teaware.bean.BillListBean;
import com.app.teaware.bean.BillListData;
import com.app.teaware.util.MyUtils;
import com.app.teaware.util.ReceiverUtils;
import com.app.teaware.util.UHFUtil;
import com.app.teaware.dialog.DialogBind;
import com.qmuiteam.qmui.skin.QMUISkinManager;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

//货品上架

public class PutUpGoodsActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.edit_qrcode)
    EditText edit_qrcode;
    BillListBean listBean;

    MyAdapterList mAdapter;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.put_up_goods_activity);
        setTitle("货品上架");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        setTitleRightText("全选", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listBean == null || listBean.getList().size()<1){
                    showToast("数据为空");
                }
                listBean.allSelect();
                setListView();
            }
        });
    }

    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(!Constants.ACTION_UHF_RESULT_SEND.equals(action))
                return ;
            Parcelable[] tagInfos =  intent.getParcelableArrayExtra(Constants.EXTRA_TAG_INFO);
            String qrcode = UHFUtil.getEPCId(tagInfos).trim().toUpperCase(Locale.ROOT);

            if(!TextUtils.isEmpty(qrcode)) {
//                View view=getWindow().getDecorView().findFocus();
                if(bindDialog != null){
                    bindDialog.setKW(qrcode);
                }else {
                    edit_qrcode.setText(qrcode);
                    getList();
                }
            }
        }//end onReceiver
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new BillListBean();
    }


    private void getList(){
        if(TextUtils.isEmpty(edit_qrcode.getText().toString())){
            showToast("请先获取箱码");
            return;
        }
        showLoading();
        MyParams params = new MyParams();
        params.put("rfid",edit_qrcode.getText().toString().trim());
        MyHttpUtils.get(Constants.URL_WORK_GET_IN_BILL_DATA, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (BillListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, BillListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.put_up_good_list_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_no)
            public TextView text_no;
            @BindView(R.id.text_code)
            public TextView text_code;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_specs)
            public TextView text_specs;
            @BindView(R.id.text_unit)
            public TextView text_unit;
            @BindView(R.id.text_kw)
            public TextView text_kw;
            @BindView(R.id.text_qrcode)
            public TextView text_qrcode;
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.img_checkbox)
            public ImageView img_check_box;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BillListData data = listBean.getList().get(position);
                text_no.setText(""+(position+1));
                MyUtils.setNotNullText(text_code,data.getGno());
                MyUtils.setNotNullText(text_name,data.getGna());
                MyUtils.setNotNullText(text_specs,data.getGsp());
                MyUtils.setNotNullText(text_unit,data.getGun());
                MyUtils.setNotNullText(text_qrcode,data.getRfid());
                MyUtils.setNotNullText(text_kw,data.getWms());

                if(data.isCheck()){
                    img_check_box.setImageResource(R.drawable.checkbox_blue_icon_selected);
                }else {
                    img_check_box.setImageResource(R.drawable.checkbox_blue_icon_unselected);
                }
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                BillListData data = (BillListData) v.getTag();
                data.setCheck(!data.isCheck());
                notifyItemChanged(getAdapterPosition());
            }


        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        ReceiverUtils.unRegisterReceiver(mContext,mResultReceiver);
    }


    @Override
    protected void onResume() {
        super.onResume();
        ReceiverUtils.registerUHFReceiver(mContext,mResultReceiver);
    }

    DialogBind bindDialog;
    @OnClick(R.id.img_bind)
    public void btnBind(){
        bindDialog= new DialogBind(mContext,listBean.getCheckBean(), new DialogBind.onPeopleStatusEditListener() {
            @Override
            public void onBind(String kw) {
                updateWms(kw);

            }
            @Override
            public void onCancel() {
                setListView();
                bindDialog = null;
            }

            @Override
            public void onToast(String mes) {
                showToast(mes);

            }
        });
        bindDialog.setSkinManager(QMUISkinManager.defaultInstance(mContext));
        bindDialog.show();
    }


    private void updateWms(String kw){
        if(TextUtils.isEmpty(kw)){
            showToast("请选择库位");
            return;
        }

        if(listBean.getCheckRfidList().size()<1){
            return;
        }
        bindDialog = null;
        MyParams params = new MyParams();
//        Gson gson = new Gson();
//        String a = gson.toJson(listBean.getCheckRfidList());
        params.put("rfid",listBean.getCheckRfidString());
        params.put("wms", kw);
        showLoading();
        MyHttpUtils.post(Constants.URL_WORK_POST_UP_DATE_WMS, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing()) return;
                        BasisBean basisBean = (BasisBean) result;
                        showToast("提交成功");
                        getList();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);

                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, BasisBean.class);
    }

}
